package edu.hawaii.duedates.tools;

//import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
//import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

//import edu.hawaii.duedates.results.ResultsList;

/**
 * The DueDatesMail class is a mailer for the DueDates system.
 * DueDatesMail employs SMTP to handle message sending.
 * An email address and smtp server is entered via the command line.
 * Currently, messages can only be sent through a mail server that does not 
 * require a password for every message sent.  
 * Therefore, the user must enter a smtp server that has the same domain as 
 * the email address used. 
 * 
 * @author Daniel Arakaki, Jeho Jung, Arthur Shum, Daniel Tian
 *
 */
public final class DueDatesMail {
  
  /** A variable for the a Session task. */
  private Session aSession;
  
  /** An object for mailing. */ 
  private static DueDatesMail aMailer;
  
  /**
   * Instantiate security to secure the password.
   */
  private DueDatesMail() {
    //empty constructor
    //Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  }
  
  /**
   * A getter for the instance of a DueDatesMail.
   * 
   * @return DueDatesMail An instance of the DueDateMail.
   */
  public static DueDatesMail getInstance() {
    if (aMailer == null) {
      DueDatesMail.aMailer = new DueDatesMail();
    }
    return DueDatesMail.aMailer;
  }
  
  /**
   * Handles the delivery of email to a given user.
   * 
   * @param sAddress The email account that sends and receives the message. 
   * @param smtpServer The email server that is responsible for sending the email.
   * @param siteInfo A String that lists the due date information.
   * @throws Exception If an error occurs while sending email.
   * @return true Mail sent was successful.
   *         false Mail delivery failed.
   */
  public boolean sendMail(String sAddress, String smtpServer, String siteInfo) throws Exception {
    Properties mailProperty = new Properties();
    mailProperty.put("mail.smtp.host", smtpServer);
    //mailProperty.put("mail.smtp.port", "465");
    //mailProperty.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    this.aSession = Session.getInstance(mailProperty);
    this.aSession.setDebug(false);
    String mailSubject = "DueDates: Items due soon";
    String bookDue = "The following items(s) are due soon.\n";
    MimeMessage dueMessage = new MimeMessage(this.aSession);
    
//    try {
     InternetAddress senderAddress = new InternetAddress(sAddress, true);
     InternetAddress receiverAddress = new InternetAddress(sAddress);
     
     dueMessage.setFrom(senderAddress);
     dueMessage.setRecipient(Message.RecipientType.TO, receiverAddress);
     dueMessage.setSubject(mailSubject);
     
     dueMessage.setSentDate(new Date());
     dueMessage.setContent(bookDue + "\n" + siteInfo, "text/plain");
     Transport.send(dueMessage);
     System.out.println("Message sent to " + sAddress);
     return true;
//    }
//    catch (MessagingException e) {
//      System.out.println("Problem with sending message " + e);
//      return false;
//    }
  }
}
